"ui";
var 背景颜色 = "#dddddd"
var 字号 = "13"
var 字体颜色 = "#dd000000"
let encodeBase64 = str => bytesToBase64String(uft8StringToBytes(str));
let decodeBase64 = str => bytesToUtf8String(stringToBase64Bytes(str));

ui.statusBarColor(背景颜色)
ui.layout(
    <ScrollView id="bg" bg="{{背景颜色}}">
    <frame>
        <vertical padding="10 10 10">
            <text size="{{字号*4}}sp" paddingTop="60" paddingLeft="40" color="{{字体颜色}}">乐抢激活码生成器</text>
            <horizontal weightSum="10" >
            <text text="*有效时长"  layout_width="0dp" layout_weight="2"/>
            <input  id="time_1" layout_width="0dp" layout_weight="8" hint="几天之后过期"/>
            </horizontal>
            <horizontal weightSum="10" >
            <text text="AndroidId(可选)"  layout_width="0dp" layout_weight="2"/>
            <input  id="android" layout_width="0dp" layout_weight="8" hint="安卓ID"/>
            </horizontal>
            <button id="star" style="Widget.AppCompat.Button.Colored" h="{{字号*11}}px" size="{{字号*2}}sp" marginTop="20" marginRight="30" marginLeft="30">生成</button>
            <input  id="kami"  hint="生成的卡密在这里"/>
        </vertical>
    </frame>
    </ScrollView>
)


ui.star.on("click", () => {
    var B = ui.time_1.getText()             //输入框2
    var AndroidId = ui.android.getText()
        var now = new Date();
        var year = now.getFullYear();       //年
        var month = now.getMonth() + 1;     //月
        var day = now.getDate();            //日
        var t=B
        for(var i=0;i<t;i++){
            day++;
            if(day==31){
                day=1;
                month++;
                if(month==12){
                    year++
                    month=1;
                    day=1;
                }
            }
        }
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (day >= 0 && day <= 9) {
            day = "0" + day;
        }

            toastLog("到期时间:"+year+"-"+month+"-"+day);
    
    var 处理 =year.toString()+month.toString()+day.toString()+03222146+AndroidId
    log(处理)

    var 加密 = encodeBase64(encodeBase64(处理))  //加密
    ui.kami.setText(加密)
    var 解密 = decodeBase64(decodeBase64(加密));  //解密
    log(解密)
    var 年 = 解密.substring(0,4);log(年);
    var 月 = 解密.substring(4,6);log(月);
    var 日 = 解密.substring(6,8);log(日);
    var 安卓ID = 解密.substring(14,50);log(安卓ID);
    log(年+"-"+月+"-"+日);
    var 参照 = 年+"-"+月+"-"+日;
    参照 = new Date(参照.replace(/-/g,"/"));
    现在 = new Date();
    var days = 参照.getTime() - 现在.getTime();
    var time = parseInt(days / (1000*60*60*24))+1
    toastLog("剩余天数:"+time+"天");
    setClip(ui.kami.text())
    });


{
function stringToBase64Bytes(str) {
    let bytes = android.util.Base64.decode(str, 0),
        bytes_arr = [];

    bytes.forEach(value => bytes_arr.push(value));

    bytes_arr.forEach((value, index) => {
        if (bytes_arr[index] < 0) bytes_arr[index] += 256;
    });

    return bytes_arr;
}


function uft8StringToBytes(str) {
    let code = encodeURI(str),
        bytes = [];

    for (let i = 0, len = code.length; i < len; i++) {
        bytes.push(code.charAt(i) === "%" ? parseInt(code.charAt(++i) + code.charAt(++i), 16) - 256 : code.charCodeAt(i));
    }

    return bytes;
}


function bytesToBase64String(bytes_arr) {
    let str = android.util.Base64.encodeToString(bytes_arr, 0);
    return str.slice(0, -(str.length % 4));
}


function bytesToUtf8String(bytes_arr) {
    let URIEncodedStr = "";
    bytes_arr.forEach(value => URIEncodedStr += "%" + value.toString(16));
    return decodeURIComponent(URIEncodedStr);
}}
